package com.wiegand.gui.button {
	import com.wiegand.sound.Soundcontroller;
	import com.caurina.transitions.Tweener;
	import com.wiegand.event.StatusEvent;
	import com.wiegand.gui.button.views.ButtonViewClasses;
	import com.wiegand.util.ColorRectangle;

	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	/**
	 * @author kwiegand
	 */
	 
	public class NavButton extends Button{

		private var _bt : Sprite;

		private var _down : MovieClip;
		private var _up : MovieClip;
		private var _over : MovieClip;
		private var _hit : Sprite;
		private var _index : int;
		
		private var _overHeight:Number;
		private var _upHeight:Number;
		
		private var _state : String;
		
		public static var BUTTON_STATUS : String = "button status";
		public static var EVENT_OPEN : String = "button open";
		private var _mouseFunction : Function;

		public function NavButton( xml : XMLList, mouseFunction:Function = null ):void{
			_mouseFunction = mouseFunction;
			super( xml ); 
		}
				
		override protected function init():void{	
			_bt = new Sprite();
			_bt.name = super._name;
			
			name = _xml.@name;
			
			var viewClass : Class = ButtonViewClasses.getClass( super._viewName );
			_view = new viewClass();
			
			_down = new MovieClip();
			_down.name = "down";
			_down = _view.getDownState( super._xml );
			_down.alpha = 0;
			_bt.addChild( _down );	
					
			_up = new MovieClip();
			_up.name = "up";
			_up = _view.getUpState(super._xml);
			_upHeight = _up.height;
			_up.alpha = .85;
			_bt.addChild( _up );
			
			_over = new MovieClip();
			_over.name = "over";
			_over = _view.getOverState(super._xml);
			_overHeight = _over.height;
			_over.alpha = 0;
			_bt.addChild( _over );
			
			_hit = ColorRectangle.create( 0xff0000, .0 );
			_hit.name="hit";
			_hit.useHandCursor = true;
			_hit.width = _up.getChildByName("bg").width;
			_hit.height = _up.getChildByName("bg").height;			
			_bt.addChild( _hit );	
			
			enable();	
			addChild( _bt );			
			_state="up";		
	
		}
		
		public function disable():void{
			_hit.buttonMode = false;
			_hit.removeEventListener( MouseEvent.CLICK, down);
			_hit.removeEventListener( MouseEvent.MOUSE_OVER, over);
			_hit.removeEventListener( MouseEvent.MOUSE_OUT, out);		
		}
		
		public function enable():void{
			_hit.buttonMode = true;
			_hit.addEventListener( MouseEvent.CLICK, down);
			_hit.addEventListener( MouseEvent.MOUSE_OVER, over);
			_hit.addEventListener( MouseEvent.MOUSE_OUT, out);		
		}
		
		public function open( delay : Number = 0 ):void{
			var foo_y : Number = this.y;
			this.y = this.y + this.height/2;			
			this.scaleY = 0.1;
			Tweener.removeTweens( this );
			Tweener.addTween( this, { alpha:1, scaleY:1, y:foo_y, delay:1.5+delay, time:0.5, transition:"easeoutbounce", onComplete:callOpen } );
		}
		
		private function callOpen():void{
			dispatchEvent( new StatusEvent( EVENT_OPEN, "open") );
		}
		
		private function down( e : MouseEvent=null ) : void {
			if(_state!="down"){
				_state="down";
		
				if(_mouseFunction!=null) _mouseFunction(false);
		
				_up.alpha = 1;
				_over.alpha = 0;
				_down.alpha = 1;
				
				if(e!=null){ 
					super.click();
					dispatchEvent( e );
					if(_xml.@clickSnd) Soundcontroller.getInstance().playEffSound(_xml.@clickSnd);
				}
			}
		}
		
		private function over( e : MouseEvent ) : void {
			if(_state!="down"){
				_state="over";
				
				if(_mouseFunction!=null) _mouseFunction(true);	
				
				removeTweens();
				
				var upbg : Sprite = _up.getChildByName("bg") as Sprite;
				Tweener.addTween(upbg,{scaleX:1.1,delay:0.0,time:0.3,transition:"easeoutback"});
				Tweener.addTween(upbg,{scaleX:1,delay:0.4,time:0.4,transition:"easeoutexpo"});
				
				var overbg : Sprite = _over.getChildByName("bg") as Sprite;
				overbg.scaleX=0.01;	
				Tweener.addTween(overbg,{scaleX:0.99,delay:0.0,time:0.5,transition:"easeoutback"});
			
				var overbar : Sprite = _over.getChildByName("bar") as Sprite;
				overbar.alpha = 1;
				
				var overtext : Sprite = _over.getChildByName("text") as Sprite;
				overtext.alpha = 1;
			
				_up.alpha = 1;
				_over.alpha = 1;
				_down.alpha = 0;
				
				if(e!=null) dispatchEvent( e );
				
				if(_xml.@overSnd) Soundcontroller.getInstance().playEffSound(_xml.@overSnd);
			}else{
	
			}
		}
		
		public function out( e : MouseEvent = null ) : void {
			if(_state!="down"){
				
				if(_mouseFunction!=null) _mouseFunction(false);
				
				_state="out";
			
				removeTweens();
			
				var upbg : Sprite = _up.getChildByName("bg") as Sprite;
				Tweener.removeTweens(upbg);
				upbg.scaleX = 1;
				
				var overbar : Sprite = _over.getChildByName("bar") as Sprite;
				overbar.alpha = 0;
				
				var overtext : Sprite = _over.getChildByName("text") as Sprite;
				overtext.alpha = 0;
				
				var overbg : Sprite = _over.getChildByName("bg") as Sprite;
				Tweener.addTween(overbg,{scaleX:0.001,delay:0.0,time:1.3,transition:"easeoutback",onComplete:hideOver});
				
				_up.alpha = .85;
				_over.alpha = 1;
				_down.alpha = 0;
				
				if(e!=null) dispatchEvent( e );
			}else{

			}
		}
		
		public function resetState():void{
			_state="up";
		}
		
		private function hideOver():void{
			_over.alpha = 0;
		}
		
		private function removeTweens():void{
			Tweener.removeTweens( _up );
			Tweener.removeTweens( _over );
			Tweener.removeTweens( _down );
			Tweener.removeTweens( _hit );
		}
		
		public function get heightDiff():Number{
			var diff : Number = _overHeight - _upHeight;
			return diff;
		}
		
		public function get heightUP():Number{
			return _upHeight;
		}
		
		public function get heightOver():Number{
			return _overHeight;
		}
		
		
		override public function get height():Number{
			return _upHeight;
		}
		
		public function getHit():Sprite{
			return _hit;
		}
		
		public function get state():String{
			return _state;
		}
		
		public function get index():int{
			return _index;
		}
		
		public function set index(i:int):void{
			_index=i;
		}
		
		override public function setActive( b : Boolean ):void{		
			_active = b;
			if( b ){
				enable();
				if(_state=="down"){
					_state="up";
					Tweener.addTween(_over,{alpha:0,delay:0,time:.2,transition:"linear"});
					Tweener.addTween(_up,{alpha:0.85,delay:0.1,time:.2,transition:"linear"});
					_down.alpha = 0;
				}
			}
			else{
				disable();
				_state="down";
		
				_up.alpha = 1;
				_over.alpha = 0;
				_down.alpha = 1;
			}
		}		
				
		override public function destroy():void{
			removeTweens();						
			removeChild( _bt );			
			_hit.removeEventListener( MouseEvent.MOUSE_DOWN, down);
			_hit.removeEventListener( MouseEvent.MOUSE_OVER, over);
			_hit.removeEventListener( MouseEvent.MOUSE_OUT, out);
			super.destroy();
		}
						
	}
	
}
